package com.legendshop.openportal.sdk.model;

import com.legendshop.openportal.sdk.model.constants.CommonStatusEnum;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.io.Serializable;

/**
 * @author jzh
 * @version 1.0.0
 * @title Result
 * @date 2024/6/18 11:33
 * @description：
 */
@Data
@ApiModel(description = "返回的结果")
public class OpenResult<D> implements Serializable {

    private static final long serialVersionUID = 5976669647855785848L;

    /**
     * 是否成功， true：成功， false：失败
     */
    @ApiModelProperty(value = "是否成功， true：成功， false：失败")
    private Boolean success;

    /**
     * 状态码
     */
    @ApiModelProperty(value = "状态码")
    private String status;

    /**
     * 在出现异常情况（非success）下，可以根据不同的status来返回不同的值，需要业务来处理。也可以把message当作json来传递对象
     */
    @ApiModelProperty(value = "返回的成功或者错误消息")
    private String message;

    /**
     * 结果
     */
    @ApiModelProperty(value = "返回的数据列表")
    private D data;

    public OpenResult() {}

    public OpenResult(Boolean success, CommonStatusEnum statusEnum, D data) {
        this.success = success;
        this.status = statusEnum.getStatus();
        this.message = statusEnum.getMessage();
        this.data = data;
    }

    public OpenResult(Boolean success, String status, String message, D data) {
        this.success = success;
        this.status = status;
        this.message = message;
        this.data = data;
    }

    public boolean isSuccess() {
        return success != null && success;
    }

    /**
     * 成功响应
     * @return
     * @param <D>
     */
    public static <D> OpenResult<D> success() {
        return success(null);
    }

    /**
     * 成功响应
     * @param data
     * @return
     * @param <D>
     */
    public static <D> OpenResult<D> success(D data) {
        return new OpenResult<>(true, CommonStatusEnum.SUCCESS, data);
    }

    /**
     * 失败响应
     * @param message
     * @return
     * @param <D>
     */
    public static <D> OpenResult<D> fail(String message) {
        return new OpenResult<>(false, CommonStatusEnum.ERROR.getStatus(), message, null);
    }

    /**
     * 失败响应
     * @param statusEnum
     * @return
     * @param <D>
     */
    public static <D> OpenResult<D> fail(CommonStatusEnum statusEnum) {
        return new OpenResult<>(false, statusEnum, null);
    }

    /**
     * 失败响应
     * @param statusEnum
     * @return
     * @param <D>
     */
    public static <D> OpenResult<D> fail(CommonStatusEnum statusEnum, String message) {
        return new OpenResult<>(false, statusEnum.getStatus(), message, null);
    }

    /**
     * 失败响应
     * @param status
     * @param message
     * @return
     * @param <D>
     */
    public static <D> OpenResult<D> fail(String status, String message) {
        return new OpenResult<>(false, status, message, null);
    }

    /**
     * 响应失败，自定义数据
     *
     * @param data
     * @return
     */
    public static OpenResult fail(OpenResult data) {
        return new OpenResult(false, data.getStatus(), data.getMessage(), null);
    }
}
